/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Dao;

import com.sgsmcan.Entidad.HistorialClinico;
import com.sgsmcan.Hibernate.HibernateUtil;
import com.sgsmcan.Utils.Utilitario;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author user
 */
public class HistorialDaoImpl extends HibernateDao<HistorialClinico> implements HistorialDao{

    @Override
    public DetachedCriteria getHistorialesPorPerro(Integer id) {
        DetachedCriteria criteria = DetachedCriteria.forClass(HistorialClinico.class,"historial")
                .createAlias("historial.perro", "can").add(Restrictions.eq("can.id", id)).add(Restrictions.eq("historial.estado",Boolean.TRUE)).addOrder(Order.desc("historial.fechaRegistro"));
        return criteria;
    }

    @Override
    public HistorialClinico getHistorial(Integer id) {
        Session session = null;
        HistorialClinico aux = null; 
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(HistorialClinico.class,"historial").createAlias("historial.perro","can").add(Restrictions.eq("historial.id", id));
            aux = (HistorialClinico) criteria.uniqueResult();
            return aux;
        }catch(HibernateException e){
            Utilitario.LOG.error("Error en HistorialDaoImpl", e);
            throw new HibernateException(e);
        }
    }
    
}
